package com.tanhua.dubbo.api;

import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public List<Comment> findComments(String movementId, CommentType comment, Integer page, Integer pageSize) {
        Query query = Query.query(Criteria
                .where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(comment.getType()))
                .skip((page - 1) * pageSize)
                .limit(pageSize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }

    @Override
    public Integer save(Comment comment) {
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        if (movement == null) {
            throw new RuntimeException("动态为空");
        }
        comment.setPublishUserId(movement.getUserId());
        mongoTemplate.save(comment);

        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", 1);
        } else {
            update.inc("loveCount", 1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement movementModify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        return movementModify.statisCount(comment.getCommentType());
    }

    @Override
    public boolean hasMovement(String movementId, Long userId, CommentType comment) {
        Query query = Query.query(Criteria
                .where("publishId").is(new ObjectId(movementId))
                .and("userId").is(userId)
                .and("commentType").is(comment.getType()));
        return mongoTemplate.exists(query, Comment.class);
    }

    @Override
    public Integer remove(Comment comment) {
        Query query = Query.query(Criteria
                .where("publishId").is(comment.getPublishId())
                .and("userId").is(comment.getUserId())
                .and("commentType").is(comment.getCommentType()));
        mongoTemplate.remove(query, Comment.class);

        query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", -1);
        } else {
            update.inc("loveCount", -1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        return modify.statisCount(comment.getCommentType());
    }


    @Override
    public Integer updateComment(Comment comment) {
        Comment comment1 = mongoTemplate.findById(comment.getPublishId(), Comment.class);
        if (comment1 == null) {
            throw new RuntimeException("评论不存在");
        }
        comment.setPublishUserId(comment1.getUserId());
        mongoTemplate.save(comment);

        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc("likeCount",1);
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Comment modify = mongoTemplate.findAndModify(query, update, options, Comment.class);
        return modify.statisCount();
    }

    @Override
    public Integer removeComment(Comment comment) {
        Query query = Query.query(Criteria
                .where("publishId").is(comment.getPublishId())
                .and("userId").is(comment.getUserId())
                .and("commentType").is(comment.getCommentType()));
        mongoTemplate.remove(query, Comment.class);

        query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc("likeCount",-1);
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Comment modify = mongoTemplate.findAndModify(query, update, options, Comment.class);
        return modify.statisCount();
    }

    @Override
    public Integer updateVideo(Comment comment) {
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
        if (video == null) {
            throw new RuntimeException("视频不存在");
        }
        comment.setPublishUserId(video.getUserId());
        mongoTemplate.save(comment);

        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", 1);
        } else {
            update.inc("loveCount", 1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Video modify = mongoTemplate.findAndModify(query, update, options, Video.class);
        return modify.statisCount(comment.getCommentType());
    }

    @Override
    public Integer removeVideo(Comment comment) {
        Query query = Query.query(Criteria
                .where("publishId").is(comment.getPublishId())
                .and("userId").is(comment.getUserId())
                .and("commentType").is(comment.getCommentType()));
        mongoTemplate.remove(query, Comment.class);

        query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", -1);
        } else {
            update.inc("loveCount", -1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Video modify = mongoTemplate.findAndModify(query, update, options, Video.class);
        return modify.statisCount(comment.getCommentType());
    }
}
